package com.gurun.codes.business.temperature_humidity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.TextureView;

import com.gurun.base.DebounceObservableTransformer;
import com.gurun.base.Logger;
import com.gurun.base.utils.ToastManager;
import com.gurun.codes.R;
import com.gurun.codes.base.BaseAppCompatActivity;
import com.gurun.codes.databinding.ActivityCommonBinding;
import com.gurun.codes.databinding.ContentTemperatureHumidityBinding;
import com.gurun.model.bean.temperature_humidity.GetTemperatureHumidityInfoResultBean;
import com.jakewharton.rxbinding2.view.RxView;

import io.reactivex.functions.Consumer;

/**
 * 温湿度
 */
public class TemperatureHumidityActivity extends BaseAppCompatActivity implements TemperatureHumidityActivityContract.TemperatureHumidityActivityView {
    private static final String ARG_DEVICE_ID = "device_id";
    private ActivityCommonBinding mBinding;
    private ContentTemperatureHumidityBinding mContentBinding;
    private TemperatureHumidityActivityContract.TemperatureHumidityActivityViewPresenter mPresenter;
    private ProgressDialog mDialog;
    private GetTemperatureHumidityInfoResultBean mDatas;
    private long mDeviceId = 0;

    public static void startActivity(Context context, long deviceId) {
        Intent intent = new Intent(context, TemperatureHumidityActivity.class);
        intent.putExtra(ARG_DEVICE_ID, deviceId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_common);
        mPresenter = new TemperatureHumidityActivityContract.TemperatureHumidityActivityViewPresenter(this);
        mPresenter.bind(this);
        initView();
        initDatas();
        initEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.unbind();
        }
    }

    @Override
    protected void initView() {
        mBinding.rlRoot.setBackgroundResource(R.mipmap.bg_temperature_humidity);
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        mContentBinding = DataBindingUtil.inflate(layoutInflater, R.layout.content_temperature_humidity, mBinding.llContent, true);
        mContentBinding.tvHistory.requestFocus();
    }

    @Override
    protected void initDatas() {
        mDeviceId = getIntent().getLongExtra(ARG_DEVICE_ID, 0);
        mPresenter.getTemperatureHumidityInfo(mDeviceId,1);//TODO TIME FLAG
    }

    private void setUpViewByDatas() {
        try {
            if (mDatas != null) {
                String temperature = mDatas.getTemperature();
                String humidity = mDatas.getHumidity();
                String pressure = mDatas.getPressure();
                if (!TextUtils.isEmpty(temperature)) {
                    mContentBinding.tvTemperatureOne.setText(temperature + "°");
                    mContentBinding.tvTemperatureTwo.setText(temperature + "°");
                }

                if (!TextUtils.isEmpty(humidity)) {
                    mContentBinding.tvHumidity.setText(humidity + "%");
                }

                if (!TextUtils.isEmpty(pressure)) {
                    mContentBinding.tvPressure.setText(pressure + "kPa");
                }
            }
        } catch (Exception e) {
            Logger.e(e.getMessage());
        }

    }

    @Override
    protected void initEvent() {
        RxView.clicks(mContentBinding.tvHistory)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        TemperatureHumidityHistoryActivity.startActivity(mContext, mDeviceId);
                    }
                });
    }

    @Override
    public void onLoading() {
        if (mDialog == null) {
            mDialog = new ProgressDialog(mContext);
            mDialog.setMessage(getString(R.string.common_loading));
        }
        mDialog.show();
    }

    @Override
    public void onLoaded(GetTemperatureHumidityInfoResultBean datas) {
        mDatas = datas;
        setUpViewByDatas();
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(mContext, msg);
    }

    @Override
    public void onComplete() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }
}
